home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / croutes.zip / GTOJ.C < prev    next >
Text File  |  1984-04-05  |  2KB  |  55 lines

  1. /*                   *** gtoj.c ***                                  */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* integer function that returns the julian date (1 = 1st day AD)    */
  6. /* associated with a gregorian date in the form mm/dd/yy.  Returns a */
  7. /* -1 if an error occured.                                           */
  8. /*                                                                   */
  9. /* Written by L. Cuthbertson, March 1983.                            */
  10. /*                                                                   */
  11. /*********************************************************************/
  12.                  
  13. #define CENTRY 19    /* current century */
  14.  
  15. long gtoj(indate)
  16. char indate[];
  17. {
  18.     static int monthd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  19.     int i;
  20.     int leapd,iyr,imo,iday;
  21.     static long cdays = 36524, ydays = 365;
  22.  
  23.     /* convert into expanded format if necessary */
  24.     if ((cvtdate(indate)) != 0) return(-1);
  25.  
  26.     /* parse gregorian date into its pieces */
  27.     sscanf(indate,"%2d%*1c%2d%*1c%2d",imo,iday,iyr);
  28.  
  29.     /* adjust month array for leap year/non-leap year */
  30.     if (iyr < 0 || iyr > 99) return(-1);
  31.     if (iyr%4 == 0 && iyr != 0 || CENTRY%4 == 0)
  32.         monthd[1] = 29;
  33.     else
  34.         monthd[1] = 28;
  35.  
  36.     /* check for invalid month */
  37.     if (imo < 1 || imo > 12) return(-1);
  38.  
  39.     /* check for invalid day */
  40.     if (iday < 1 || iday > monthd[imo-1]) return(-1);
  41.  
  42.     /* determine the number of "extra" leap years caused by the  */
  43.     /* %400 criteria and add to it the number of leap years that */
  44.     /* has occured up to the prior year of current century.      */
  45.     leapd = CENTRY/4;
  46.     if (iyr != 0) leapd += (iyr-1)/4;
  47.  
  48.     /* determine number of days elapsed in current year */
  49.     for (i=0;i<(imo-1);i++)
  50.         iday = iday + monthd[i];
  51.  
  52.     /* calculate julian date */
  53.     return (CENTRY*cdays + iyr*ydays + leapd + iday);
  54. }
  55.